<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class MyPromise {
        constructor(fn) {
          this._fn = fn;
          this.status = "pending";
          this.callback_queue = [];
          this._fn(MyPromise.resolve.bind(this));
        }
        then(callback) {
          return new MyPromise((resolve) => {
            if (this.status === "fullfield") {
              this.value = callback(this.value);
              resolve(this.value);
            } else if (this.status === "pending") {
              this.callback_queue.push((value) => {
                this.value = callback(value);
                resolve(this.value);
              });
            }
          });
        }
        static resolve(value) {
          this.status = "fullfield";
          this.value = value;
          this.callback_queue.forEach((callback) => {
            callback(this.value);
          });
        }
      }

      new MyPromise((resolve, reject) => {
        resolve(1);
      })
        .then((val) => {
          console.log(val);
          return new MyPromise((resolve) => {
            setTimeout(() => {
              resolve("aaa");
            }, 1000);
          });
        })
        .then((val) => {
          console.log(val);
        });
    </script>
  </body>
</html>
